'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    // 创建标签表
    await queryInterface.createTable('tags', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
      },
      name: {
        type: Sequelize.STRING(50),
        allowNull: false,
        comment: '标签名称',
      },
      type: {
        type: Sequelize.ENUM('color', 'style', 'occasion', 'season', 'material', 'brand', 'custom'),
        allowNull: false,
        comment: '标签类型',
      },
      color: {
        type: Sequelize.STRING(7),
        allowNull: true,
        comment: '标签颜色',
      },
      description: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '标签描述',
      },
      sort_order: {
        type: Sequelize.INTEGER,
        defaultValue: 0,
        comment: '排序',
      },
      is_active: {
        type: Sequelize.BOOLEAN,
        defaultValue: true,
        comment: '是否启用',
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
      },
    });

    // 创建衣服标签关联表
    await queryInterface.createTable('clothing_tags', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
      },
      clothing_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: '衣服ID',
        references: {
          model: 'clothing',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
      },
      tag_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: '标签ID',
        references: {
          model: 'tags',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
      },
    });

    // 创建搭配衣服关联表
    await queryInterface.createTable('outfit_items', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
      },
      outfit_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: '搭配ID',
        references: {
          model: 'outfits',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
      },
      clothing_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: '衣服ID',
        references: {
          model: 'clothing',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
      },
      position: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: '位置',
      },
      layer_order: {
        type: Sequelize.INTEGER,
        defaultValue: 1,
        comment: '层级顺序',
      },
      notes: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '备注',
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
      },
    });

    // 添加索引
    await queryInterface.addIndex('tags', ['type']);
    await queryInterface.addIndex('tags', ['is_active']);
    await queryInterface.addIndex('clothing_tags', ['clothing_id']);
    await queryInterface.addIndex('clothing_tags', ['tag_id']);
    await queryInterface.addIndex('outfit_items', ['outfit_id']);
    await queryInterface.addIndex('outfit_items', ['clothing_id']);
    
    // 添加唯一约束
    await queryInterface.addIndex('clothing_tags', ['clothing_id', 'tag_id'], {
      unique: true,
      name: 'unique_clothing_tag'
    });
    await queryInterface.addIndex('outfit_items', ['outfit_id', 'clothing_id'], {
      unique: true,
      name: 'unique_outfit_clothing'
    });
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('outfit_items');
    await queryInterface.dropTable('clothing_tags');
    await queryInterface.dropTable('tags');
  },
};
